<?php
namespace app\admin\controller;
use app\common\model\ManagerGongzi;
use app\common\model\ManagerRole;
use app\common\model\Menu as MenuModel;
use app\common\model\Manager as ManagerModel;
use \app\common\model\Role;

class Manager extends Base
{
    public function __construct () {
        parent::__construct();
    }

    /**
     * @return \think\response\Json
     * 管理员信息
     */
    public function index(){
        //查询条件
        $where = [
            'id' => input('token')
        ];
        //管理员信息
        $manager = ManagerModel::getOne($where);

        //管理员角色id
        $m_r_w = array(
            'manager_id' => array('eq',$manager['id']),
        );

        //角色信息
        $role_ids = ManagerRole::getColumn($m_r_w,'role_id');
        if($role_ids){
            $r_w = array(
                'id' => array('in',$role_ids)
            );
            $manager['role_name'] = Role::getColumn($r_w,'role_name');
            $manager['roles'] = $role_ids;
        }else{
            $manager['role_name'] = array();
            $manager['roles'] = array();
        }
        //菜单
        $list = MenuModel::getColumn([],'*','pid asc,id asc');
        $menus = create_tree_list(0,$list);

        //返回信息
        return returnJson(0,'success',array(
            'user'=>$manager,
            'menus'=>$menus
        ));
    }

    /**
     * 获取角色为教练的所有员工
     * @return void
     */
    public function coach(){
        //查询-获取所有教练id
        $mids_where = [
            'role_id' => 3
        ];
        $mids = ManagerRole::getColumn($mids_where,'manager_id');

        //查询-获取所有教练信息
        $where = array(
            'id' => array('in',$mids)
        );
        $list = ManagerModel::getAll($where,'id desc','email,id');

        //返回信息
        return returnJson(0,'success',$list);
    }


    public function list(){
        $where = [
            'status' => array('eq',1)
        ];
        $page = max(input('page'),1);
        $size = 20;
        $list = ManagerModel::getPage($where,$page,$size);
        $total = ManagerModel::getCount($where);

        foreach ($list as $k => $v){
            $m_r_w = array(
                'manager_id' => $v['id'],
            );
            $role_ids = ManagerRole::getColumn($m_r_w,'role_id');
            if($role_ids){
                $r_w = array(
                    'id' => array('in',$role_ids)
                );
                $list[$k]['roles'] = Role::getColumn($r_w,'role_name');
                $list[$k]['role_ids'] = $role_ids;
            }else{
                $list[$k]['roles'] = array();
                $list[$k]['role_ids'] = array();
            }
        }
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }

    /*
     列表
     */
    public function managerAllList(){
        $where = [
            'mall_id' => $this->manager['mall_id']
        ];
        $managerList = $this->managerModel->getAll($where);

        return returnJson(0,'',$managerList);
    }

    /* 新增管理员/修改管理员 */
    public function edit () {
        if(request()->isPost()) {
            $input = input();
            $data = array(
                'email' => $input['zhanghao'],
                'update_time' => time()
            );
            if($input['mima']!='') {
                if(checkPassword($input['mima'])<3) {
                    return returnJson(1,'密码过于简单，建议使用数字字母组合密码');
                }
                $data['password'] = createPassword($input['mima']);
            }
            if($input['id']) {
                $data['id'] = $input['id'];
            }

            $nameOld = ManagerModel::getOne(array('email'=>$input['zhanghao']));
            if($nameOld&&$nameOld['id']!=$input['id']) {
                return returnJson(1,'账号已存在');
            }

            if($data['id']) {
                $res = ManagerModel::update($data,array('id'=>$data['id']));
                $res = $data['id'];
            }else{
                $data['create_time'] = time();
                $res = ManagerModel::setInsertGetId($data);
                //添加本月工资条
                $save = array(
                    'jichu' => config('jichu_gongzi'),
                    'zhaoxin' => 0,
                    'keshi' => 0,
                    'time' => date('Y-m'),
                    'mid' => $res,
                    'name' => $input['zhanghao'],
                    'create_time' => time(),
                    'update_time' => time()
                );
                ManagerGongzi::setInsertGetId($save);
            }

            $role_ids = input('roles/a');
            $role_save = [];
            foreach ($role_ids as $role_id) {
                $role_save[] = array(
                    'role_id' => $role_id,
                    'manager_id' => $res,
                    'create_time' => time(),
                    'update_time' => time()
                );
            }
            ManagerRole::setDel(array('manager_id'=>$res));
            ManagerRole::setSaveAll($role_save);

            if($res) {
                return returnJson(0,'操作成功');
            }
            return returnJson(1,'网络异常，请重试');
        }
    }


    /* 删除 */
    public function delete () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        $manager = ManagerModel::getOne(['id'=>$input['id']]);
        if(!$manager) {
            return returnJson(1,'管理员不存在');
        }
        $res = ManagerModel::update(array('status'=>0),array('id'=>$input['id']));
        if($res) {
            return returnJson(0,'已删除');
        }else{
            return returnJson(1,'删除失败，请重试');
        }
    }


    /* 权限组 */
    public function menuList () {
        $where = [
            'status' => 1,
            'level' => 1
        ];
        if($this->manager['mall_id']==0) {
            $where['is_admin'] = 1;
        }else{
            $where['is_mall'] = 1;
        }
        $menus = MenuModel::getAll($where,'ord desc,id asc');
        foreach($menus as $k=>$v) {
            $subwhere = [
                'status' => 1,
                'is_limit' => 1,
                'controller' => $v['controller']
            ];
            if($this->manager['mall_id']==0) {
                $subwhere['is_admin'] = 1;
            }else{
                $subwhere['is_mall'] = 1;
            }
            $subList = MenuModel::getAll($subwhere,'ord desc,id asc');
            if($subList) {
                $menus[$k]['children'] = $subList;
            }
        }
        return returnJson(0,'',$menus);
    }
}
